//
//  FoodMateriaViewController.m
//  FoodList
//
//  Created by lanou on 16/4/8.
//  Copyright © 2016年 王金晶. All rights reserved.
//

#import "FoodMateriaViewController.h"
#import "FoodMaterialTableViewCell.h"
#import "RequestManger.h"
#import "FoodMaterialModel.h"
#import "FoodDetailViewController.h"

@interface FoodMateriaViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIButton *vegetableButton;
@property (weak, nonatomic) IBOutlet UIButton *fruitButton;
@property (weak, nonatomic) IBOutlet UIButton *meatButton;
@property (weak, nonatomic) IBOutlet UIButton *riceButton;


@property(nonatomic,strong)NSMutableArray *modelArray;

@end

@implementation FoodMateriaViewController

#pragma mark ----- 数据请求 -----
-(void)requestwithNumber:(NSInteger)number
{
    NSDictionary *parDic = @{@"sct":[NSNumber numberWithInteger:number]};
    [RequestManger requestWithURL:@"http://api.meishi.cc/v5/season_list.php?format=json" requestType:@"RequestPOST" parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSArray *objArray = dic[@"obj"];
        _modelArray = [NSMutableArray array];
        for (NSDictionary *modelDic in objArray) {
            FoodMaterialModel *model = [[FoodMaterialModel alloc]init];
            [model setValuesForKeysWithDictionary:modelDic];
            [_modelArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [_tableView reloadData];
        });
        
    } error:^(NSError *error) {
        NSLog(@"数据请求错误");
    }];
}

#pragma mark -------------- viewDidLoad ---------
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"食材精选";

    // 一上来加载的是蔬菜界面
    [_vegetableButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self requestwithNumber:2];
    
    
}
#pragma mark ---------- button点击事件------------------
- (IBAction)vegetable:(id)sender {
    [_vegetableButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_fruitButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_meatButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_riceButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self requestwithNumber:2];
    
}
- (IBAction)fruit:(id)sender {
    _fruitButton.userInteractionEnabled = YES;
    [_fruitButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_vegetableButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_meatButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_riceButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self requestwithNumber:1];
}
- (IBAction)meat:(id)sender {
    [_meatButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_vegetableButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_fruitButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_riceButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self requestwithNumber:3];
}
- (IBAction)rice:(id)sender {
    [_riceButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_vegetableButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_fruitButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_meatButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self requestwithNumber:4];
}


#pragma mark -------- tableView -------------

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _modelArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FoodMaterialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"food"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"FoodMaterialTableViewCell" owner:nil options:nil] firstObject];
    }
    FoodMaterialModel *model = _modelArray[indexPath.row];
    [cell setModel:model];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FoodDetailViewController *foodDetail = [[FoodDetailViewController alloc]init];
    // 点击cell的时候将id 和 title 传给下个界面
    FoodMaterialModel *model = _modelArray[indexPath.row];
    foodDetail.idStr = model.myID;
    foodDetail.titleStr = model.title;
    foodDetail.heatStr = model.heat_word;
    foodDetail.gongxiaoArray = model.gongxiao;
    
    [self.navigationController pushViewController:foodDetail animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
